import { paramsAryToStr } from '@/utils/paramsFormat';
import { CURRENT_NODE_MODULE } from '@/utils/const';
import { expeditedOptions } from '@/filters/status';

export default {
  data() {
    return {
      serializerKeys: [],
      timeTypeKeys: [], // 时间类型
      statusListBf: [
        { value: 0, label: '草稿中' },
        { value: 1, label: '审批中' },
        { value: 2, label: '未过审' },
        { value: 3, label: '已作废' },
        { value: 4, label: '已完成' },
        { value: 5, label: '待提交' }
      ]
    };
  },
  computed: {
    fixedKeys() { // 固定筛选配置项
      return ['currentNode', 'currentHandler', 'status', 'isExpedited', 'comment', 'approvalComment'];
    }
  },
  methods: {
    // 设置高级筛选项
    setFilterList(list) {
      let filterList = [];
      list.forEach(item => {
        const filterItem = {
          keyword: item.key,
          name: item.name,
          _type: item.type
        };
        if (this.fixedKeys.includes(item.key)) {
          switch (item.key) {
            case 'currentNode':
              filterItem.type = 'loadMoreSelect';
              filterItem.valueName = 'id';
              filterItem.labelName = 'id';
              filterItem.directShow = true;
              filterItem.multiple = true;
              filterItem.extendParams = { type: CURRENT_NODE_MODULE.CUSTOM_PROCESS, id: this.moduleId };
              filterItem.placeholder = '当前节点';
              filterItem.searchkeyword = 'keyword';
              filterItem.apiUrl = 'admin/common/currentNodes';
              this.serializerKeys.push(item.key);
              break;
            case 'currentHandler':
              filterItem.type = 'input';
              break;
            case 'status':
              filterItem.type = 'select';
              filterItem.options = this.statusList || this.statusListBf;
              break;
            case 'isExpedited':
              filterItem.type = 'select';
              filterItem.options = expeditedOptions;
              break;
          }
        } else {
          switch (item.type) {
            case 'formulaInput':
            case 'link':
            case 'input':
              filterItem.type = item['isNumber'] ? 'inputRange' : 'input';
              filterItem['isNumber'] = item['isNumber'];
              if (filterItem.type === 'inputRange') {
                filterItem.startPlaceholder = '最低数值';
                filterItem.endPlaceholder = '最高数值';
                this.serializerKeys.push(item.key);
              }
              break;
            case 'statistics':
              filterItem.isNumber = true;
              filterItem.type = 'inputRange';
              filterItem.startPlaceholder = '最低数值';
              filterItem.endPlaceholder = '最高数值';
              this.serializerKeys.push(item.key);
              break;
            case 'user':
            case 'textarea':
            case 'workflowId':
            case 'createUser':
            case 'post':
              filterItem.type = 'input';
              break;
            case 'select':
              filterItem.type = item.isCascader ? 'cascaderSelect' : 'mulSelect';
              filterItem.options = item.options || [];
              filterItem.childrenVal = 'children';
              filterItem.valueName = 'id';
              filterItem.labelName = 'id';
              this.serializerKeys.push(item.key);
              break;
            case 'radio':
            case 'checkbox':
              filterItem.type = 'mulSelect';
              filterItem.options = item.options || [];
              filterItem.valueName = 'id';
              filterItem.labelName = 'id';
              this.serializerKeys.push(item.key);
              break;
            case 'date':
            case 'createTime':
              filterItem.type = 'rangeDate';
              if (item.format === 'yyyy/MM/dd') {
                filterItem.timeType = 'daterange';
              } else if (item.format === 'yyyy/MM') {
                filterItem.timeType = 'monthrange';
                filterItem.startPlaceholder = '开始月份';
                filterItem.endPlaceholder = '结束月份';
              } else if (item.format === 'yyyy/MM/dd HH:mm:ss') {
                filterItem.startPlaceholder = '开始时间';
                filterItem.endPlaceholder = '结束时间';
              }
              filterItem.format = item.format;
              this.timeTypeKeys.push({ key: item.key, formatType: item.format });
              this.serializerKeys.push(item.key);
              break;
            case 'switch':
              filterItem.type = 'select';
              filterItem.options = [
                { label: '是', value: true },
                { label: '否', value: false }
              ];
              break;
            case 'department':
              filterItem.type = 'cascaderSelectWithChild';
              filterItem.childrenVal = 'children';
              filterItem.options = this.deptTreeList;
              this.serializerKeys.push(item.key);
              break;
            case 'address':
              filterItem.type = 'addressSelect';
              filterItem.addressType = item.options && item.options.addressType ? item.options.addressType : 'region';
          }
        }
        filterList.push(filterItem);
      });
      filterList = filterList.concat([
        { keyword: 'comment', name: '评论', type: 'input' },
        { keyword: 'approvalComment', name: '审批意见', type: 'input' }
      ]);
      return filterList;
    },
    // 获取过滤数据的原始类型和是否为数值型
    getFilterType(key) {
      const index = this.filterList.findIndex(item => item.keyword === key);
      const params = { type: null, isNumber: false };
      if (index !== -1) {
        params.type = this.filterList[index]._type;
        params.isNumber = !!this.filterList[index]['isNumber'];
      }
      return params;
    },
    // 结束时间戳二次处理
    formatTimeType(list, inputData = null) {
      const filterParams = _.cloneDeep(inputData || this.paramsObj);
      for (const key of Object.keys(filterParams)) {
        const arrEmpty = Array.isArray(filterParams[key]) && filterParams[key].length === 0;
        const rangeArrEmpty = Array.isArray(filterParams[key]) && filterParams[key].length === 2 && _.isNil(filterParams[key][0]) && _.isNil(filterParams[key][1]);
        if (arrEmpty || rangeArrEmpty) {
          delete filterParams[key];
        }
      }
      if (Array.isArray(list) && list.length) {
        list.forEach(item => {
          const filterArr = filterParams[item.key];
          if (Array.isArray(filterArr) && filterArr.length) {
            if (item.formatType === 'yyyy/MM/dd') {
              filterArr[1] += 86399000;
            } else if (item.formatType === 'yyyy/MM') {
              const year = new Date(filterArr[1]).getFullYear();
              const month = new Date(filterArr[1]).getMonth();
              const endDate = new Date(new Date(year, month + 1, 1).setHours(0, 0, 0, 0) - 1);
              filterArr[1] = endDate.getTime();
            }
          }
          filterParams[item.key] = filterArr;
        });
      }
      return filterParams;
    },
    setDynamicFilter(inputData = null) {
      const params = {};
      const filterParams = this.formatTimeType(this.timeTypeKeys, inputData);
      paramsAryToStr(this.serializerKeys, filterParams);
      const dynamicFilterList = [];
      const dynamicKeys = Object.keys(filterParams);
      dynamicKeys.forEach(key => {
        // 固定筛选配置项不放入动态搜索字段中
        if (this.fixedKeys.includes(key)) {
          params[key] = filterParams[key];
        } else if (filterParams[key] || filterParams[key] === 0 || filterParams[key] === false) {
          const { type, isNumber } = this.getFilterType(key);
          dynamicFilterList.push({
            key,
            type,
            isNumber,
            value: filterParams[key]
          });
        }
      });
      params.dynamicFilter = JSON.stringify(dynamicFilterList);
      return params;
    }
  }
};
